package demo09;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author JYH
 * @version 1.0
 * @date 2022/11/1 17:12
 */
public class TicketLockTest2 {
    public static void main(String[] args) {
        Ticket2 ticket = new Ticket2();

        Thread thread1 = new Thread(ticket,"窗口A");
        Thread thread2 = new Thread(ticket,"窗口B");
        Thread thread3 = new Thread(ticket,"窗口C");
        Thread thread4 = new Thread(ticket,"窗口D");

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();

    }
}

class Ticket2 implements Runnable{
    private int tickets=100;
    Lock lock = new ReentrantLock();
    @Override
    public void run() {
        while (true){
                try {
                    lock.lock();
                    if (tickets > 0) {
                        System.out.println(Thread.currentThread().getName() + "卖了一张票,剩余" + --tickets + "张");
                    } else {
                        break;
                    }
                }finally {
                    lock.unlock();
                }


        }
    }
}